AddCSLuaFile("cl_init.lua")
AddCSLuaFile("cl_heart.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
 
function GM:PlayerLoadout(ply)
	return
end

/*---------------------------------------------------------
   Weapons & playermodels system when you start the game by phpmysql
---------------------------------------------------------*/

function GM:PlayerSpawn(ply)
	self.BaseClass:PlayerSpawn(ply)
	if ply:Team() == TEAM_BLUE then
		ply:SetModel("models/player/Group01/male_02.mdl") -- DEFAULT PLAYERMODEL SPECTERS TEAM
		ply:SetNWFloat( "Pulse", 0 );
	
	//ply:SetHealth(100) -- DEFAULT  HEALTH,RUNSPEED AND WALKSPEED  SPECTERS TEAM ( change the player's pulse here)
	//ply:SetRunSpeed(700) 
	//ply:SetWalkSpeed(400)
	
		ply:Give("deagle") --- DEFAULT WEAPONS FOR THE  SPECTERS TEAM
		ply:Give("thompson");
		ply:Give("winchester");
		ply:GiveAmmo(30,"smg1");
		ply:GiveAmmo(6,"357");
		ply:GiveAmmo(5,"Buckshot");
	else
		ply:SetModel("models/player/soldier_stripped.mdl") -- DEFAULT PLAYERMODEL MEDDLERS TEAM
	
	//ply:SetHealth(100) -- DEFAULT  HEALTH,RUNSPEED AND WALKSPEED  MEDDLERS TEAM ( change the player's pulse here)
	//ply:SetRunSpeed(700) 
	//ply:SetWalkSpeed(400)
	
		--ply:Give("empty_weapon") -- DEFAULT WEAPONS FOR THE  MEDDLERS TEAM
	end
end


 /*---------------------------------------------------------    
 Name: gamemode:EntityTakeDamage( entity, inflictor, attacker, amount )    
 Desc: The entity has received damage	
 -------------------------------------------------------*/  
function GM:EntityTakeDamage( ent, inflictor, attacker, amount )     	
	if ( ent:IsValid() and ent:IsPlayer() and ent:Team() == "TEAM_BLUE" ) then
		current = ent:GetNWFloat( "Pulse" )
		ent:SetNWFloat( "Pulse", (current - amount) );
	end
end

/*---------------------------------------------------------
   Friendly Fire is off by default
---------------------------------------------------------*/

function GM:PlayerShouldTakeDamage(victim,attacker)
	if( attacker:IsValid() and attacker:IsPlayer() and ( victim:Team() == attacker:Team() ) ) then
		return false
	end
	return true
end

/*---------------------------------------------------------
   Text when you chose another team. delete this to avoid change class during the game
---------------------------------------------------------*/

function ChooseHumans( p )
	if ( p:Team( )== TEAM_BLUE ) then
		p:PrintMessage(HUD_PRINTTALK,"You're already a Meddler!");
	else
		p:SetTeam( TEAM_BLUE )
		p:Kill()
		p:SprintEnable()
		p:AddFrags(1)
		hook.Remove( "HUDPaint", "HUD_TEST2" );
	end
end
concommand.Add( "human", ChooseHumans );


function ChooseGhosts( p )
	if ( p:Team( )== TEAM_RED ) then
		p:PrintMessage(HUD_PRINTTALK,"You're already a Scepter!");
	else
		p:SetTeam( TEAM_RED )
		p:Kill()
		p:SprintEnable()
		p:AddFrags(1)
		hook.Remove( "HUDPaint", "HUD_TEST2" );
	end
end
concommand.Add( "ghost", ChooseGhosts );

--[[function GM:KeyPress(p, key) 
	if ( key == IN_SPEED ) then 
		umsg.Start("cl_teammenu", pl) 
		umsg.End()
		p:PrintMessage(HUD_PRINTTALK,"It worked");
	end
	self.BaseClass:KeyPress(p, key)
end]]--

/*---------------------------------------------------------
   Text when you start the game
---------------------------------------------------------*/

function GM:PlayerInitialSpawn( pl )
	
	pl:PrintMessage(HUD_PRINTCENTER,"Welcome to Ghosts Ghouls Gamemode!");
	
	self.BaseClass:PlayerInitialSpawn(pl)
	
	umsg.Start("cl_teammenu", pl) 
	umsg.End() 
	
	
end

/*---------------------------------------------------------
   Player Spawn is counter strike spawn points by blackops
---------------------------------------------------------*/

function GM:PlayerSelectSpawn(pl)
	if self.redSpawns == nil then
		self.redSpawns = table.Add(self.redSpawns,ents.FindByClass("info_player_terrorist"))
	end
	if self.blueSpawns == nil then
		self.blueSpawns = table.Add(self.blueSpawns,ents.FindByClass("info_player_counterterrorist"))
	end
	
	local numSpwn = 0
	local cSpawnList = nil
	if pl:Team() == TEAM_BLUE then
		cSpawnList = self.blueSpawns
	else
		cSpawnList = self.redSpawns
	end
	
	numSpwn = table.Count(cSpawnList)
	if numSpwn == 0 then
		Msg("PlayerSelectSpawn error: no spawn available\n")
		return nil
	end
	
	local ChosenSpawnPoint = nil
	
	for i=0,6 do
		ChosenSpawnPoint = cSpawnList[math.random(1,numSpwn)]
		if ChosenSpawnPoint && ChosenSpawnPoint:IsValid() && ChosenSpawnPoint:IsInWorld() && ChosenSpawnPoint != pl:GetVar( "LastSpawnpoint" ) then	
			pl:SetVar( "LastSpawnpoint", ChosenSpawnPoint )
			return ChosenSpawnPoint
		end
	end
	return ChosenSpawnPoint
end


/*---------------------------------------------------------
   Map Rotation system By gamerdude
---------------------------------------------------------*/

local mRotateTimeLeft=1200 --this is the round time in seconds (3600= 1 hr)
mapsList={"ggg_mansion","ggg_manor","ggg_industrialpark"} -- ie. mapsList={"gm_construct","gm_flatgrass"}
timeWarnings={}
timeWarnings[900]="15 Minute Warning!" --  this is the time warnings to add, put line under other one and do timeWarnings[<seconds left to do warnings>]="<warning message>"
timeWarnings[5]="5"
timeWarnings[4]="4"
timeWarnings[3]="3"
timeWarnings[2]="2"
timeWarnings[1]="1"

if(SERVER) then
function nextMap()
	cMapName=string.lower(game.GetMap())
	cMapID=1
	nMap="gm_construct"

	for k,v in pairs(mapsList) do
		if(string.lower(v)==cMapName) then
			cMapID=k
			if(k==table.Count(mapsList)) then
				nMap=mapsList[1]
			else
				nMap=mapsList[k+1]
			end
		end
	end
	
	return nMap
end
function MapRotate()
	local nMap=nextMap()
	game.ConsoleCommand("changelevel " .. nMap .. "\n")
end

function setcountdown()
	if(GetGlobalInt("MapRotateCountDown")<1) then
		MapRotate()
		return
	end
	local cTime=GetGlobalInt("MapRotateCountDown")-1
	SetGlobalInt("MapRotateCountDown",cTime)
	SetGlobalString("MapRotateCountDownFormat", SecondsToFormat(cTime))
	if(timeWarnings[cTime]!=null) then
		for k,v in pairs(player.GetAll()) do
			v:PrintMessage(HUD_PRINTCENTER, timeWarnings[cTime])
		end
	end
end

function SecondsToFormat( secs )
	local hours=math.floor(secs/60/60)
	local mins=math.floor(secs/60)
	local seconds=0
	local timeleft=""
	if not(hours==0) then
		timeleft=hours .. ":"
	end
	if not(mins==0) then
		timeleft=timeleft .. mins .. ":"
	else
		timeleft=timeleft .. "00:"
	end
	seconds=secs - (hours*3600) - (mins*60)
	if (seconds<10) then
		seconds="0" .. seconds
	end
	timeleft=timeleft .. seconds
	
	return timeleft
end

timeWarnings[7]="Switching to " .. nextMap() .. " in..."
SetGlobalInt("MapRotateCountDown",mRotateTimeLeft)
SetGlobalString("MapRotateCountDownFormat", SecondsToFormat(mRotateTimeLeft))
timer.Create("MapRotateCountDown", 1, 0, setcountdown)
end

--[[local function Pulse( pl, _, args )

	args = tonumber( args[ 1 ] ) or 0
	
	pl:SetNWFloat( "Pulse", args )
end

concommand.Add( "/pulse", Pulse )
]]--




